<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>头条全文检索实现 | 课程简介</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C10-Elasticsearch/Suggest.html" />
    
    
    <link rel="prev" href="../C10-Elasticsearch/Query.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="10.8"
        data-chapter-title="头条全文检索实现"
        data-filepath="C10-Elasticsearch/ToutiaoSearchImplement.md"
        data-basepath=".."
        data-revision="Mon May 27 2019 03:35:45 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="C01-Introduction/index.html">
            
                
                    <a href="../C01-Introduction/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        项目介绍
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="C01-Introduction/AboutToutiaoWebVM.html">
            
                
                    <a href="../C01-Introduction/AboutToutiaoWebVM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        ToutiaoWeb虚拟机使用说明
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="C01-Introduction/PycharmRemoteDevelop.html">
            
                
                    <a href="../C01-Introduction/PycharmRemoteDevelop.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        Pycharm远程开发
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="C01-Introduction/AboutProject.html">
            
                
                    <a href="../C01-Introduction/AboutProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        产品与开发
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C02-Database/index.html">
            
                
                    <a href="../C02-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C02-Database/DatabaseDesign.html">
            
                
                    <a href="../C02-Database/DatabaseDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        数据库设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C02-Database/ORM.html">
            
                
                    <a href="../C02-Database/ORM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        理解ORM
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C02-Database/SQLAlchemyMapping.html">
            
                
                    <a href="../C02-Database/SQLAlchemyMapping.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        SQLAlchemy映射构建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C02-Database/SQLAlchemyOperations.html">
            
                
                    <a href="../C02-Database/SQLAlchemyOperations.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        SQLAlchemy操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="C02-Database/DatabaseTheory.html">
            
                
                    <a href="../C02-Database/DatabaseTheory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        数据库理论
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="C02-Database/DistributedID.html">
            
                
                    <a href="../C02-Database/DistributedID.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        分布式ID
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="C02-Database/DatabaseOptimize.html">
            
                
                    <a href="../C02-Database/DatabaseOptimize.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        数据库优化
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="C02-Database/Redis.html">
            
                
                    <a href="../C02-Database/Redis.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        Redis
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C03-Gitflow/index.html">
            
                
                    <a href="../C03-Gitflow/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        Git工作流
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C03-Gitflow/Gitflow.html">
            
                
                    <a href="../C03-Gitflow/Gitflow.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Gitflow
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C03-Gitflow/AboutGit.html">
            
                
                    <a href="../C03-Gitflow/AboutGit.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Git总结
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C03-Gitflow/ProjectDirectory.html">
            
                
                    <a href="../C03-Gitflow/ProjectDirectory.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        工程目录
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="C03-Gitflow/DebugMethod.html">
            
                
                    <a href="../C03-Gitflow/DebugMethod.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        调试方法
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C04-JWT/index.html">
            
                
                    <a href="../C04-JWT/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        JWT
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C04-JWT/JWT.html">
            
                
                    <a href="../C04-JWT/JWT.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        JWT &amp; JWS &amp; JWE
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="C04-JWT/PythonLib.html">
            
                
                    <a href="../C04-JWT/PythonLib.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        PythonLib
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C04-JWT/Implement.html">
            
                
                    <a href="../C04-JWT/Implement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        实施方案
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C04-JWT/Forbbiden.html">
            
                
                    <a href="../C04-JWT/Forbbiden.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        禁用问题
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C05-OSS/index.html">
            
                
                    <a href="../C05-OSS/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        对象存储
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C05-OSS/Qiniu.html">
            
                
                    <a href="../C05-OSS/Qiniu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        七牛云存储
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C05-OSS/CDN.html">
            
                
                    <a href="../C05-OSS/CDN.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        CDN
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C06-Cache/index.html">
            
                
                    <a href="../C06-Cache/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        缓存
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C06-Cache/CacheArchitecture.html">
            
                
                    <a href="../C06-Cache/CacheArchitecture.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        缓存的架构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="C06-Cache/CacheData.html">
            
                
                    <a href="../C06-Cache/CacheData.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存的数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="C06-Cache/CacheTTLAndEviction.html">
            
                
                    <a href="../C06-Cache/CacheTTLAndEviction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        缓存的有效期与淘汰策略
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="C06-Cache/CachePatterns.html">
            
                
                    <a href="../C06-Cache/CachePatterns.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        缓存模式
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="C06-Cache/CacheProblems.html">
            
                
                    <a href="../C06-Cache/CacheProblems.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        缓存问题
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="C06-Cache/ProjectCacheAndStorageDesign.html">
            
                
                    <a href="../C06-Cache/ProjectCacheAndStorageDesign.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        项目缓存与Redis持久存储设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="C06-Cache/ProjectCacheImplement.html">
            
                
                    <a href="../C06-Cache/ProjectCacheImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.7.</b>
                        
                        项目缓存实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="C06-Cache/ProjectRedisStorageImplement.html">
            
                
                    <a href="../C06-Cache/ProjectRedisStorageImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.8.</b>
                        
                        项目Redis持久存储实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C07-APScheduler/index.html">
            
                
                    <a href="../C07-APScheduler/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        APScheduler
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C07-APScheduler/APSchedulerTutorial.html">
            
                
                    <a href="../C07-APScheduler/APSchedulerTutorial.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        APScheduler使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C07-APScheduler/FixStatistics.html">
            
                
                    <a href="../C07-APScheduler/FixStatistics.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定时修正统计数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C08-RPC/index.html">
            
                
                    <a href="../C08-RPC/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        RPC
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C08-RPC/AboutRPC.html">
            
                
                    <a href="../C08-RPC/AboutRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        RPC简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C08-RPC/gRPC.html">
            
                
                    <a href="../C08-RPC/gRPC.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        gRPC
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="C08-RPC/ProtocolBuffers.html">
            
                
                    <a href="../C08-RPC/ProtocolBuffers.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        Protocol Buffers
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C08-RPC/RecoSysIDL.html">
            
                
                    <a href="../C08-RPC/RecoSysIDL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        推荐系统接口定义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C08-RPC/Server.html">
            
                
                    <a href="../C08-RPC/Server.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        服务端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.6" data-path="C08-RPC/Client.html">
            
                
                    <a href="../C08-RPC/Client.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.6.</b>
                        
                        客户端
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.7" data-path="C08-RPC/ToutiaoAPIViewImplement.html">
            
                
                    <a href="../C08-RPC/ToutiaoAPIViewImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.7.</b>
                        
                        头条首页新闻推荐接口编写
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="9" data-path="C09-IM/index.html">
            
                
                    <a href="../C09-IM/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.</b>
                        
                        即时通信
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="9.1" data-path="C09-IM/AboutIM.html">
            
                
                    <a href="../C09-IM/AboutIM.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.1.</b>
                        
                        即时通信简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.2" data-path="C09-IM/WebSocket.html">
            
                
                    <a href="../C09-IM/WebSocket.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.2.</b>
                        
                        WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.3" data-path="C09-IM/SocketIO.html">
            
                
                    <a href="../C09-IM/SocketIO.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.3.</b>
                        
                        Socket.IO
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.4" data-path="C09-IM/Chat.html">
            
                
                    <a href="../C09-IM/Chat.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.4.</b>
                        
                        头条聊天服务实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="9.5" data-path="C09-IM/MessagePush.html">
            
                
                    <a href="../C09-IM/MessagePush.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>9.5.</b>
                        
                        头条在线消息推送实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="10" data-path="C10-Elasticsearch/index.html">
            
                
                    <a href="../C10-Elasticsearch/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.</b>
                        
                        Elasticsearch
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="10.1" data-path="C10-Elasticsearch/Introduction.html">
            
                
                    <a href="../C10-Elasticsearch/Introduction.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.1.</b>
                        
                        简介与原理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.2" data-path="C10-Elasticsearch/ConceptAndCluster.html">
            
                
                    <a href="../C10-Elasticsearch/ConceptAndCluster.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.2.</b>
                        
                        概念与集群
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.3" data-path="C10-Elasticsearch/AnalysisIK.html">
            
                
                    <a href="../C10-Elasticsearch/AnalysisIK.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.3.</b>
                        
                        IK中文分析器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.4" data-path="C10-Elasticsearch/IndexAndType.html">
            
                
                    <a href="../C10-Elasticsearch/IndexAndType.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.4.</b>
                        
                        索引与类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.5" data-path="C10-Elasticsearch/Document.html">
            
                
                    <a href="../C10-Elasticsearch/Document.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.5.</b>
                        
                        文档
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.6" data-path="C10-Elasticsearch/Logstash.html">
            
                
                    <a href="../C10-Elasticsearch/Logstash.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.6.</b>
                        
                        Logstash导入数据
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.7" data-path="C10-Elasticsearch/Query.html">
            
                
                    <a href="../C10-Elasticsearch/Query.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.7.</b>
                        
                        查询
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="10.8" data-path="C10-Elasticsearch/ToutiaoSearchImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSearchImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.8.</b>
                        
                        头条全文检索实现
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.9" data-path="C10-Elasticsearch/Suggest.html">
            
                
                    <a href="../C10-Elasticsearch/Suggest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.9.</b>
                        
                        suggest查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="10.10" data-path="C10-Elasticsearch/ToutiaoSuggestImplement.html">
            
                
                    <a href="../C10-Elasticsearch/ToutiaoSuggestImplement.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>10.10.</b>
                        
                        头条suggest查询实现
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="11" data-path="C11-Unittest.html">
            
                
                    <a href="../C11-Unittest.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>11.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12" data-path="C12-Deployment/index.html">
            
                
                    <a href="../C12-Deployment/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.</b>
                        
                        部署相关
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="12.1" data-path="C12-Deployment/Gunicorn.html">
            
                
                    <a href="../C12-Deployment/Gunicorn.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.1.</b>
                        
                        Gunicorn
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="12.2" data-path="C12-Deployment/Supervisor.html">
            
                
                    <a href="../C12-Deployment/Supervisor.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>12.2.</b>
                        
                        Supervisor
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >课程简介</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5934;&#x6761;&#x5168;&#x6587;&#x68C0;&#x7D22;&#x5B9E;&#x73B0;">&#x5934;&#x6761;&#x5168;&#x6587;&#x68C0;&#x7D22;&#x5B9E;&#x73B0;</h1>
<h3 id="elasticsearch-python&#x5BA2;&#x6237;&#x7AEF;&#x4F7F;&#x7528;">elasticsearch python&#x5BA2;&#x6237;&#x7AEF;&#x4F7F;&#x7528;</h3>
<p><a href="https://elasticsearch-py.readthedocs.io/en/master/&gt;" target="_blank">https://elasticsearch-py.readthedocs.io/en/master/&gt;</a></p>
<pre><code class="lang-shell">pip install elasticsearch
</code></pre>
<p>&#x5BF9;&#x4E8E;elasticsearch 5.x &#x7248;&#x672C; &#x9700;&#x8981;&#x6309;&#x4EE5;&#x4E0B;&#x65B9;&#x5F0F;&#x5BFC;&#x5165;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> elasticsearch5 <span class="hljs-keyword">import</span> Elasticsearch

<span class="hljs-comment"># elasticsearch&#x96C6;&#x7FA4;&#x670D;&#x52A1;&#x5668;&#x7684;&#x5730;&#x5740;</span>
ES = [
    <span class="hljs-string">&apos;127.0.0.1:9200&apos;</span>
]

<span class="hljs-comment"># &#x521B;&#x5EFA;elasticsearch&#x5BA2;&#x6237;&#x7AEF;</span>
es = Elasticsearch(
    ES,
    <span class="hljs-comment"># &#x542F;&#x52A8;&#x524D;&#x55C5;&#x63A2;es&#x96C6;&#x7FA4;&#x670D;&#x52A1;&#x5668;</span>
    sniff_on_start=<span class="hljs-keyword">True</span>,
    <span class="hljs-comment"># es&#x96C6;&#x7FA4;&#x670D;&#x52A1;&#x5668;&#x7ED3;&#x70B9;&#x8FDE;&#x63A5;&#x5F02;&#x5E38;&#x65F6;&#x662F;&#x5426;&#x5237;&#x65B0;es&#x7ED3;&#x70B9;&#x4FE1;&#x606F;</span>
    sniff_on_connection_fail=<span class="hljs-keyword">True</span>,
    <span class="hljs-comment"># &#x6BCF;60&#x79D2;&#x5237;&#x65B0;&#x7ED3;&#x70B9;&#x4FE1;&#x606F;</span>
    sniffer_timeout=<span class="hljs-number">60</span>
)
</code></pre>
<p>&#x641C;&#x7D22;&#x4F7F;&#x7528;&#x65B9;&#x5F0F;</p>
<pre><code class="lang-python">query = {
    <span class="hljs-string">&apos;query&apos;</span>: {
        <span class="hljs-string">&apos;bool&apos;</span>: {
            <span class="hljs-string">&apos;must&apos;</span>: [
                {<span class="hljs-string">&apos;match&apos;</span>: {<span class="hljs-string">&apos;_all&apos;</span>: <span class="hljs-string">&apos;python web&apos;</span>}}
            ],
            <span class="hljs-string">&apos;filter&apos;</span>: [
                {<span class="hljs-string">&apos;term&apos;</span>: {<span class="hljs-string">&apos;status&apos;</span>: <span class="hljs-number">2</span>}}
            ]
        }
    }
}
ret = es.search(index=<span class="hljs-string">&apos;articles&apos;</span>, doc_type=<span class="hljs-string">&apos;article&apos;</span>, body=query)
</code></pre>
<h3 id="&#x5934;&#x6761;&#x9879;&#x76EE;&#x641C;&#x7D22;&#x63A5;&#x53E3;&#x89C6;&#x56FE;&#x5B9E;&#x73B0;">&#x5934;&#x6761;&#x9879;&#x76EE;&#x641C;&#x7D22;&#x63A5;&#x53E3;&#x89C6;&#x56FE;&#x5B9E;&#x73B0;</h3>
<p>&#x5728;toutiao-backend/toutiao/resources/search&#x76EE;&#x5F55;&#x4E2D;&#x65B0;&#x5EFA;search.py</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> flask_restful <span class="hljs-keyword">import</span> Resource
<span class="hljs-keyword">from</span> flask_restful.reqparse <span class="hljs-keyword">import</span> RequestParser
<span class="hljs-keyword">from</span> flask_restful <span class="hljs-keyword">import</span> inputs
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> g, current_app
<span class="hljs-keyword">from</span> redis.exceptions <span class="hljs-keyword">import</span> RedisError

<span class="hljs-keyword">from</span> . <span class="hljs-keyword">import</span> constants
<span class="hljs-keyword">from</span> cache <span class="hljs-keyword">import</span> article <span class="hljs-keyword">as</span> cache_article
<span class="hljs-keyword">from</span> cache <span class="hljs-keyword">import</span> user <span class="hljs-keyword">as</span> cache_user
<span class="hljs-keyword">from</span> models.user <span class="hljs-keyword">import</span> Search
<span class="hljs-keyword">from</span> models <span class="hljs-keyword">import</span> db

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">SearchResource</span><span class="hljs-params">(Resource)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;
    &#x641C;&#x7D22;&#x7ED3;&#x679C;
    &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;
        &#x83B7;&#x53D6;&#x641C;&#x7D22;&#x7ED3;&#x679C;
        &quot;&quot;&quot;</span>
        qs_parser = RequestParser()
        qs_parser.add_argument(<span class="hljs-string">&apos;q&apos;</span>, type=inputs.regex(<span class="hljs-string">r&apos;^.{1,50}$&apos;</span>), required=<span class="hljs-keyword">True</span>, location=<span class="hljs-string">&apos;args&apos;</span>)
        qs_parser.add_argument(<span class="hljs-string">&apos;page&apos;</span>, type=inputs.positive, required=<span class="hljs-keyword">False</span>, location=<span class="hljs-string">&apos;args&apos;</span>)
        qs_parser.add_argument(<span class="hljs-string">&apos;per_page&apos;</span>, type=inputs.int_range(constants.DEFAULT_SEARCH_PER_PAGE_MIN, constants.DEFAULT_SEARCH_PER_PAGE_MAX, <span class="hljs-string">&apos;per_page&apos;</span>), required=<span class="hljs-keyword">False</span>, location=<span class="hljs-string">&apos;args&apos;</span>)
        args = qs_parser.parse_args()
        q = args.q
        page = <span class="hljs-number">1</span> <span class="hljs-keyword">if</span> args.page <span class="hljs-keyword">is</span> <span class="hljs-keyword">None</span> <span class="hljs-keyword">else</span> args.page
        per_page = args.per_page <span class="hljs-keyword">if</span> args.per_page <span class="hljs-keyword">else</span> constants.DEFAULT_SEARCH_PER_PAGE_MIN

        <span class="hljs-comment"># Search from Elasticsearch</span>
        query = {
            <span class="hljs-string">&apos;from&apos;</span>: (page-<span class="hljs-number">1</span>)*per_page,
            <span class="hljs-string">&apos;size&apos;</span>: per_page,
            <span class="hljs-string">&apos;_source&apos;</span>: <span class="hljs-keyword">False</span>,
            <span class="hljs-string">&apos;query&apos;</span>: {
                <span class="hljs-string">&apos;bool&apos;</span>: {
                    <span class="hljs-string">&apos;must&apos;</span>: [
                        {<span class="hljs-string">&apos;match&apos;</span>: {<span class="hljs-string">&apos;_all&apos;</span>: q}}
                    ],
                    <span class="hljs-string">&apos;filter&apos;</span>: [
                        {<span class="hljs-string">&apos;term&apos;</span>: {<span class="hljs-string">&apos;status&apos;</span>: <span class="hljs-number">2</span>}}
                    ]
                }
            }
        }
        ret = current_app.es.search(index=<span class="hljs-string">&apos;articles&apos;</span>, doc_type=<span class="hljs-string">&apos;article&apos;</span>, body=query)

        total_count = ret[<span class="hljs-string">&apos;hits&apos;</span>][<span class="hljs-string">&apos;total&apos;</span>]

        results = []

        hits = ret[<span class="hljs-string">&apos;hits&apos;</span>][<span class="hljs-string">&apos;hits&apos;</span>]
        <span class="hljs-keyword">for</span> result <span class="hljs-keyword">in</span> hits:
            article_id = int(result[<span class="hljs-string">&apos;_id&apos;</span>])
            article = cache_article.ArticleInfoCache(article_id).get()
            <span class="hljs-keyword">if</span> article:
                results.append(article)

        <span class="hljs-comment"># Record user search history</span>
        <span class="hljs-keyword">if</span> g.user_id <span class="hljs-keyword">and</span> page == <span class="hljs-number">1</span>:
            <span class="hljs-keyword">try</span>:
                cache_user.UserSearchingHistoryStorage(g.user_id).save(q)
            <span class="hljs-keyword">except</span> RedisError <span class="hljs-keyword">as</span> e:
                current_app.logger.error(e)

        <span class="hljs-keyword">return</span> {<span class="hljs-string">&apos;total_count&apos;</span>: total_count, <span class="hljs-string">&apos;page&apos;</span>: page, <span class="hljs-string">&apos;per_page&apos;</span>: per_page, <span class="hljs-string">&apos;results&apos;</span>: results}
</code></pre>
<p>&#x5728;toutiao-backend/toutiao/resources/search&#x76EE;&#x5F55;&#x4E2D;&#x65B0;&#x5EFA;constants.py</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x641C;&#x7D22;&#x7ED3;&#x679C;&#x5206;&#x9875;&#x9ED8;&#x8BA4;&#x6BCF;&#x9875;&#x6570;&#x91CF; &#x4E0B;&#x9650;</span>
DEFAULT_SEARCH_PER_PAGE_MIN = <span class="hljs-number">10</span>

<span class="hljs-comment"># &#x641C;&#x7D22;&#x7ED3;&#x679C;&#x9875;&#x9ED8;&#x8BA4;&#x6BCF;&#x9875;&#x6570;&#x91CF; &#x4E0A;&#x9650;</span>
DEFAULT_SEARCH_PER_PAGE_MAX = <span class="hljs-number">50</span>
</code></pre>
<h3 id="&#x6DFB;&#x52A0;es&#x65B0;&#x6587;&#x7AE0;&#x7D22;&#x5F15;&#x6570;&#x636E;">&#x6DFB;&#x52A0;ES&#x65B0;&#x6587;&#x7AE0;&#x7D22;&#x5F15;&#x6570;&#x636E;</h3>
<p>&#x5728;&#x81EA;&#x5A92;&#x4F53;&#x5E73;&#x53F0;&#x53D1;&#x5E03;&#x6587;&#x7AE0;&#x63A5;&#x53E3;&#x4E2D;&#xFF0C;&#x9664;&#x4E86;&#x4FDD;&#x5B58;&#x6587;&#x7AE0;&#x5916;&#xFF0C;&#x8FD8;&#x8981;&#x5411;es&#x5E93;&#x4E2D;&#x6DFB;&#x52A0;&#x65B0;&#x6587;&#x7AE0;&#x7684;&#x7D22;&#x5F15;</p>
<pre><code class="lang-python">doc = {
          <span class="hljs-string">&apos;article_id&apos;</span>: article.id,
          <span class="hljs-string">&apos;user_id&apos;</span>: article.user_id,
          <span class="hljs-string">&apos;title&apos;</span>: article.title,
          <span class="hljs-string">&apos;content&apos;</span>: article.content.content,
          <span class="hljs-string">&apos;status&apos;</span>: article.status,
          <span class="hljs-string">&apos;create_time&apos;</span>: article.ctime
      }
current_app.es.index(index=<span class="hljs-string">&apos;articles&apos;</span>, doc_type=<span class="hljs-string">&apos;article&apos;</span>, body=doc, id=article.id)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C10-Elasticsearch/Query.html" class="navigation navigation-prev " aria-label="Previous page: 查询"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C10-Elasticsearch/Suggest.html" class="navigation navigation-next " aria-label="Next page: suggest查询"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
